$(document).ready(function() {
    dataSource = new kendo.data.DataSource({
        transport: {
            create: {
                url: appBosch.crud.create,
                dataType: "json"
            },
            read: {
                url: appBosch.crud.read,
                dataType: "json"
            },
            update: {
                url: appBosch.crud.update,
                dataType: "json"
            },
            destroy: {
                url: appBosch.crud.destroy,
                dataType: "json"
            },
            parameterMap: function(options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'create':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'read':
                        break;
                    case 'update':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'destroy':
                        params = {id: options.models[0].id};
                        break;
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id",
                fields: {
                    test_id: {
                        editable: true,
                        nullable: false,
                        defaultValue: appBosch.tests[0].id
                    },
                    user_id: {
                        editable: false,
                        nullable: false,
                        defaultValue: appBosch.userId
                    },
                    application_date: {
                        editable: true,
                        nullable: false,
                        type: "date",
                        validation: {
                            required: true
                        }
                    },
                    calification: {
                        editable: true,
                        nullable: false,
                        type: "number",
                        validation: {
                            required: true,
                            min: 0.0
                        }
                    }
                }
            }
        }
    });

    $("#grid").kendoGrid({
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        sortable: {
            mode: "single",
            allowUnsort: false
        },
        pageable: true,
        dataSource: dataSource,
        height: 520,
        columns: [
            {
                values: appBosch.tests,
                field: "test_id",
                title: "Examen"
            },
            {
                values: appBosch.users,
                field: "user_id",
                title: "Usuario"
            },
            {
                field: "application_date",
                title: "Fecha"
            },
            {
                field: "calification",
                title: "Calificación"
            },
            {command: [
                    {text: "Detalle", click: showDetail},
                    {
                        name: "edit",
                        text: {
                            edit: "",
                            update: "",
                            cancel: ""
                        }
                    },
                    {
                        name: "destroy",
                        text: ""
                    }], title: "&nbsp;", width: 300}],
        editable: {
            confirmation: "¿Estas seguro que deseas eliminar este curso?",
            mode: "popup"
        }
    });
});

function showDetail(e)
{
    e.preventDefault();
    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var userId = dataItem.user_id;
    var testId = dataItem.test_id;
    var url = appBosch.urlDetail + '/' + userId + '/' + testId;
    window.open(url);
}